using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

namespace EGEngine
{
    public class AudioAgent : MonoBehaviour
    {
        [SerializeField] private AudioSource audioSource;
        public bool IsUsing;
        public EAudioType AudioType;
        public string Location;
        public int fadeInOutState;
        public float originVolume;

        public async Task PlayAsync(ResourceLoader loader, string location, EAudioType audioType, float volume,
            bool loop, bool thisFadeIn)
        {
            Location = location;
            AudioType = audioType;
            originVolume = volume;

            if (thisFadeIn)
            {
                fadeInOutState = 1;
                audioSource.volume = volume * AudioModule.FadeOutVolumePercent;
            }

            var clip = await loader.LoadAsync<AudioClip>(location);
            name = $"{audioType}_{location}_{clip.name}_Loop:{loop}";
            audioSource.loop = loop;
            audioSource.PlayOneShot(clip, volume);
        }

        public void PlaySync(ResourceLoader loader, string location, EAudioType audioType, float volume,
            bool loop, bool thisFadeIn)
        {
            Location = location;
            AudioType = audioType;
            originVolume = volume;

            if (thisFadeIn)
            {
                fadeInOutState = 1;
                audioSource.volume = volume * AudioModule.FadeOutVolumePercent;
            }

            var clip = loader.LoadSync<AudioClip>(location);
            name = $"{audioType}_{location}_{clip.name}_Loop:{loop}";
            audioSource.loop = loop;
            audioSource.PlayOneShot(clip, volume);
        }

        public void SetFadeInOut(int state)
        {
            fadeInOutState = state;
        }

        private void Update()
        {
            if (fadeInOutState == 1)
            {
                audioSource.volume += originVolume * AudioModule.FadeOutVolumePercent / AudioModule.FadeInTime *
                                      Time.deltaTime;
                if (audioSource.volume > originVolume)
                {
                    audioSource.volume = originVolume;
                    fadeInOutState = 0;
                }
            }
            else if (fadeInOutState == -1)
            {
                audioSource.volume -= originVolume * AudioModule.FadeOutVolumePercent / AudioModule.FadeOutTime *
                                      Time.deltaTime;
                if (audioSource.volume < originVolume * AudioModule.FadeOutVolumePercent)
                {
                    audioSource.volume = originVolume * AudioModule.FadeOutVolumePercent;
                    fadeInOutState = 0;
                }
            }
        }

        public static AudioAgent CreateAgent()
        {
            var go = new GameObject("agent");
            var audioSource = go.AddComponent<AudioSource>();
            audioSource.playOnAwake = false;
            var agent = go.AddComponent<AudioAgent>();
            agent.audioSource = audioSource;
            return agent;
        }
    }
}